package com.lineage.data.item_etcitem.add;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.skill.L1BuffUtil;
import com.lineage.server.serverpackets.S_ChangeHeading;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_EffectLocation;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class newskillbook extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(newskillbook.class);
  
  private Random randon = new Random();
  
  private int _range = 0;
  
  private int _count = 0;
  
  private int _dmgMin = 0;
  
  private int _dmgMax = 0;
  
  private int _gfxId = 0;
  
  private int _itemId = 0;
  
  private int _itemCount = 0;
  
  public static ItemExecutor get() {
    return new newskillbook();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int spellsc_objid = data[0];
    int spellsc_x = data[1];
    int spellsc_y = data[2];
    L1BuffUtil.cancelAbsoluteBarrier(pc);
    L1Object target = World.get().findObject(spellsc_objid);
    if (target != null || this._count > 1) {
      doAction(pc, target);
    } else {
      pc.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(spellsc_x, spellsc_y, pc.getMapId()), 10), 7);
    } 
  }
  
  private boolean check(L1PcInstance user, L1Object target) {
    if (user.getId() == target.getId())
      return false; 
    if (!user.glanceCheck(target.getX(), target.getY()))
      return false; 
    int location = target.getLocation().getTileLineDistance((Point)user.getLocation());
    if (location > this._range) {
      user.sendPackets((ServerBasePacket)new S_SystemMessage("距离为:" + this._range));
      _log.info("距离为:" + this._range);
      return false;
    } 
    return true;
  }
  
  private void doAction(L1PcInstance user, L1Object target) {
    Random _random = new Random();
    if (this._count == 1 && !check(user, target))
      return; 
    int dmg = 0;
    if (this._dmgMax - this._dmgMin <= 0) {
      dmg = this._dmgMin;
    } else {
      dmg = _random.nextInt(this._dmgMax - this._dmgMin) + this._dmgMin;
    } 
    if (this._itemId != 0 && !user.getInventory().consumeItem(this._itemId, this._itemCount)) {
      user.sendPackets((ServerBasePacket)new S_SystemMessage("施法材料不足"));
      return;
    } 
    if (this._count > 1) {
      ArrayList<L1Object> list = World.get().getVisibleObjects((L1Object)user, this._count);
      if (list != null) {
        Iterator<L1Object> iterator = list.iterator();
        while (iterator.hasNext()) {
          L1Object object = iterator.next();
          if (object instanceof L1PcInstance) {
            L1PcInstance pc = (L1PcInstance)object;
            if (pc.getMap().isSafetyZone((Point)pc.getLocation()) || pc.checkNonPvP(user, (L1Character)pc))
              return; 
            if (pc.hasSkillEffect(50) || user.hasSkillEffect(78) || pc.hasSkillEffect(157))
              return; 
            if (pc.hasSkillEffect(68))
              dmg /= 2; 
            int newHp = pc.getCurrentHp() - dmg;
            if (newHp <= 0 && !pc.isGm())
              pc.death((L1Character)user); 
            pc.setCurrentHp(newHp);
            pc.sendPacketsX10((ServerBasePacket)new S_DoActionGFX(pc.getId(), 2));
            continue;
          } 
          if (!(object instanceof L1MonsterInstance))
            continue; 
          L1MonsterInstance mob = (L1MonsterInstance)object;
          mob.receiveDamage((L1Character)user, dmg);
          mob.broadcastPacketX10((ServerBasePacket)new S_DoActionGFX(mob.getId(), 2));
        } 
        user.sendPacketsX10((ServerBasePacket)new S_SkillSound(user.getId(), this._gfxId));
      } 
    } else {
      if (target instanceof L1PcInstance) {
        L1PcInstance pc2 = (L1PcInstance)target;
        if (pc2.getMap().isSafetyZone((Point)pc2.getLocation()) || user.checkNonPvP(user, (L1Character)pc2))
          return; 
        if (pc2.hasSkillEffect(50) || pc2.hasSkillEffect(78) || pc2.hasSkillEffect(157))
          return; 
        if (pc2.hasSkillEffect(68))
          dmg /= 2; 
        int newHp2 = pc2.getCurrentHp() - dmg;
        if (newHp2 <= 0 && !pc2.isGm())
          pc2.death((L1Character)user); 
        pc2.setCurrentHp(newHp2);
      } else if (target instanceof L1MonsterInstance) {
        L1MonsterInstance mob2 = (L1MonsterInstance)target;
        mob2.receiveDamage((L1Character)user, dmg);
      } 
      user.setHeading(user.targetDirection(target.getX(), target.getY()));
      user.sendPacketsX10((ServerBasePacket)new S_ChangeHeading((L1Character)user));
      user.sendPacketsX10((ServerBasePacket)new S_DoActionGFX(user.getId(), 17));
      if (target instanceof L1PcInstance) {
        L1PcInstance tgpc = (L1PcInstance)target;
        tgpc.sendPacketsX10((ServerBasePacket)new S_SkillSound(tgpc.getId(), this._gfxId));
        tgpc.sendPacketsX10((ServerBasePacket)new S_DoActionGFX(tgpc.getId(), 2));
      } else if (target instanceof L1MonsterInstance) {
        L1MonsterInstance mob2 = (L1MonsterInstance)target;
        mob2.broadcastPacketX10((ServerBasePacket)new S_SkillSound(mob2.getId(), this._gfxId));
        mob2.broadcastPacketX10((ServerBasePacket)new S_DoActionGFX(mob2.getId(), 2));
      } else {
        user.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(target.getX(), target.getY(), user.getMapId()), 
              this._gfxId), 7);
      } 
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._range = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
    try {
      this._count = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
    try {
      this._dmgMin = Integer.parseInt(set[3]);
    } catch (Exception exception) {}
    try {
      this._dmgMax = Integer.parseInt(set[4]);
    } catch (Exception exception) {}
    try {
      this._gfxId = Integer.parseInt(set[5]);
    } catch (Exception exception) {}
    try {
      this._itemId = Integer.parseInt(set[6]);
    } catch (Exception exception) {}
    try {
      this._itemCount = Integer.parseInt(set[7]);
    } catch (Exception exception) {}
  }
}
